package com.yiwei.zxp;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.InitAppBean;
import com.yiwei.zxp.api.model.InitAppModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.fragment.MainHomeFragment;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.DownloadUtil;
import com.yiwei.zxp.util.FileUtils;
import com.yiwei.zxp.util.PackageUtil;
import com.yiwei.zxp.util.PreferenceUtils;
import com.yiwei.zxp.util.RBInfo;

import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;

public class MainActivity extends BaseActivity {

    @BindView(R.id.viewpager)
    ViewPager viewpager;


    private Unbinder unbiner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBarTouming();
        unbiner = ButterKnife.bind(this);

        Fragment[] fragments = new Fragment[]{new MainHomeFragment()};
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }

        });
//
//        tabBottom.setupWithViewPager(viewpager);
//        tabBottom.getTabAt(0).select();
//        initRequest();
    }

    private void initRequest() {
//        if (!Network.checkNetwork(this)) {
//            ToastUtil.show(this, "网络连接失败");
//            return;
//        }

        InitAppBean bean = new InitAppBean();
        bean.client_type = "app";
        bean.device_id = RBInfo.getInstance().getUniquePsuedoID();

        bean.user_agent = RBInfo.getInstance().getUserAgent();

        bean.os = "Android";
        bean.os_version = RBInfo.getInstance().getOSVersion();
//        Log.i("dadaddas",RBInfo.getInstance().getNativePhoneNumber());
        bean.browser = "android";
        bean.browser_version = "1.0";
        bean.mobile_brand = RBInfo.getInstance().getBrand();
        bean.mobile_model = RBInfo.getInstance().getModel();
        bean.app_version = PackageUtil.getVersionName(this);
//        double lat = PreferenceUtils.getDouble(BaseContext.getApplicationContext(), Const.KEY.LAT, -1);
//        double lng = PreferenceUtils.getDouble(BaseContext.getApplicationContext(), Const.KEY.LNG, -1);
//        if (lat > 0 && lng > 0) {
        bean.lat = "";
        bean.lang = "";
//        PreferenceUtils.putString(this, "lat", lat + "");
//        PreferenceUtils.putString(this, "lng", lang + "");

//        }

        Api.api_service.getInitInfo(bean).compose(RxLifeUtil.NoErrorOn(this))
                .doOnError(throwable -> {
//                    Util.startActivity(this, MainActivity.class);
//                    finish();
                })
                .onErrorResumeNext(new Function<Throwable, ObservableSource<? extends InitAppModel>>() {
                    @Override
                    public ObservableSource<? extends InitAppModel> apply(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                        return Observable.empty();
                    }
                }).
                subscribe(result -> {
                    UserInfoGlobal.saveInitData(result.datas);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }




}
